package com.fengke.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.fengke.dao.member.MemberManager;
import com.fengke.dao.model.MemberModel;
import com.fengke.dao.model.OpenUnionModel;
import com.fengke.service.MemberService;
import com.fengke.service.vo.MemberVO;
import com.fengke.service.vo.OpenUnionVO;

public class MemberServiceImpl implements MemberService {
	private MemberManager memberManager;
	public MemberVO researchMemberByUnionId(String unionId) throws Exception {
		MemberModel memberModel = new MemberModel();
		memberModel.setUnionId(unionId);
		memberModel = memberManager.researchMemberByUnionId(memberModel);
		MemberVO memberVO = new MemberVO();
		memberVO.setMemberModel(memberModel);
		return memberVO;
	}
	public Long createMember(MemberVO memberVO) throws Exception {
		return memberManager.createMember(memberVO.getMemberModel());
	}
	@Override
	public OpenUnionVO researchOpenUnionByOpenId(String openId) throws Exception {
		OpenUnionModel openUnionModel = new OpenUnionModel();
		openUnionModel.setOpenId(openId);
		openUnionModel = memberManager.researchOpenUnionByOpenId(openUnionModel);
		OpenUnionVO openUnionVO = new OpenUnionVO();
		openUnionVO.setOpenUnionModel(openUnionModel);
		return openUnionVO;
	}
	@Override
	public Long createOpenUnion(OpenUnionVO openUnionVO) throws Exception {
		return memberManager.createOpenUnion(openUnionVO.getOpenUnionModel());
	}
	public Integer researchMemberListSize(MemberVO memberVO) throws Exception {
		if(memberVO.getMemberModel() == null) {
			memberVO.setMemberModel(new MemberModel());
		}
		return memberManager.researchMemberListSize(memberVO.getMemberModel());
	}
	public List<MemberVO> researchMemberList(MemberVO memberVO, int offset, int limit) throws Exception {
		if(memberVO.getMemberModel() == null) {
			memberVO.setMemberModel(new MemberModel());
		}
		List<MemberModel> memberModelList = memberManager.researchMemberList(memberVO.getMemberModel(), offset, limit);
		if(memberModelList != null && memberModelList.size() > 0) {
			List<MemberVO> memberVOList = new ArrayList<MemberVO>();
			for(MemberModel model : memberModelList) {
				MemberVO vo = new MemberVO();
				vo.setMemberModel(model);
				memberVOList.add(vo);
			}
			return memberVOList;
		}
		return null;
	}
	public MemberManager getMemberManager() {
		return memberManager;
	}
	public void setMemberManager(MemberManager memberManager) {
		this.memberManager = memberManager;
	}
}
